/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.eb.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.eb.entity.Classify;
import com.kaaction.modules.eb.entity.Product;
import com.kaaction.modules.eb.entity.ProductClassify;
import com.kaaction.modules.eb.service.ClassifyService;
import com.kaaction.modules.eb.service.ProductClassifyService;
import com.kaaction.modules.eb.service.ProductService;
import com.kaaction.modules.operate.entity.ClientMaterial;
import com.kaaction.modules.operate.entity.Material;

/**
 * 商品Controller
 * @author zengbiao
 * @version 2017-04-19
 */
@Controller
@RequestMapping(value = "${adminPath}/eb/product")
public class ProductController extends BaseController {

	@Autowired
	private ProductService productService;
	
	@Autowired
	private ClassifyService classifyService;
	
	@Autowired
	private ProductClassifyService productClassifyService;
	
	@ModelAttribute
	public Product get(@RequestParam(required=false) String id) {
		Product entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = productService.get(id);
		}
		if (entity == null){
			entity = new Product();
		}
		return entity;
	}
	
	@RequiresPermissions("eb:product:view")
	@RequestMapping(value = {"list", ""})
	public String list(Product product, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Product> page = productService.findPage(new Page<Product>(request, response), product); 
		model.addAttribute("page", page);
		return "modules/eb/productList";
	}

	@RequiresPermissions("eb:product:view")
	@RequestMapping(value = "form")
	public String form(Product product, Model model) {
		
		
		if (!StringUtils.isBlank(product.getId())) {
			List<ProductClassify> productClassifyIds = productClassifyService.findListByProductId(product.getId());
			String classifyIds = "";
			for(ProductClassify ins : productClassifyIds){
				classifyIds += ("," + ins.getClassify().getId());
			}
			product.setClassifyIds(classifyIds);
		}
		model.addAttribute("product", product);
		//生成树状结构的渠道列表
		model.addAttribute("classifyList", classifyService.findList(new Classify()));

		return "modules/eb/productForm";
	}

	@RequiresPermissions("eb:product:edit")
	@RequestMapping(value = "save")
	public String save(Product product, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, product)){
			return form(product, model);
		}
		
		productService.save(product);
		
		if(product != null && !StringUtils.isBlank(product.getId())){
			//1.delete all related records
			productClassifyService.deleteByProductId(product.getId());
			
			String classifyIds = product.getClassifyIds();
			//2.add 
			if(!StringUtils.isBlank(classifyIds) && classifyIds.indexOf(",") != -1){
				String[] classifyIdArr = classifyIds.split(",");
				for(String classifyIdStr : classifyIdArr) {
					ProductClassify productClassify = new ProductClassify();
					if(!StringUtils.isBlank(classifyIdStr)){
						productClassify.setProduct(product);
						Classify classify = new Classify();
						classify.setId(classifyIdStr);
						productClassify.setClassify(classify);
						productClassifyService.save(productClassify);
					}
				}
			}
		} 
		
		
		addMessage(redirectAttributes, "保存商品成功");
		return "redirect:"+Global.getAdminPath()+"/eb/product/?repage";
	}
	
	@RequiresPermissions("eb:product:edit")
	@RequestMapping(value = "delete")
	public String delete(Product product, RedirectAttributes redirectAttributes) {
		productService.delete(product);
		addMessage(redirectAttributes, "删除商品成功");
		return "redirect:"+Global.getAdminPath()+"/eb/product/?repage";
	}

}